/* 
 * RecordFieldReader.java 13.07.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Helper class to read bytes out of a database file.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class RecordFieldReader {
    /** the position in the byte array */
    private int pos = 1;

    /** the input data */
    private byte[] data;

    /** character encoding */
    private static final String ENCODING = "US-ASCII";

    public RecordFieldReader(byte[] data) {
	this.data = data;
    }

    /**
     * Converts the required number of bytes into a String.
     * 
     * @param length
     *            the length to be converted from current offset.
     * @return the converted String
     * @throws UnsupportedEncodingException
     */
    public String read(int length) throws UnsupportedEncodingException {
	String str = new String(data, pos, length, ENCODING);
	pos += length;
	return str.trim();
    }

    /**
     * Reads the first byte.
     * 
     * @return the first byte
     * @throws IOException
     */
    public int readByte() throws IOException {
	return data[0];
    }
}
